#include <stdio.h>
#include <stdlib.h>
#include "Includes/Vector.h"
#include "Includes/Matrix.h"
#include <unistd.h>
#include <math.h>

/// <summary>
/// TODO
/// </summary>
int GalMatrixIdentity(MatrixPtr mtx)
{
	if (mtx == NULL)
		return -1;

	mtx->m11 = 1.0f;
	mtx->m12 = 0.0f;
	mtx->m13 = 0.0f;
	mtx->m14 = 0.0f;
	mtx->m21 = 0.0f;
	mtx->m22 = 1.0f;
	mtx->m23 = 0.0f;
	mtx->m24 = 0.0f;
	mtx->m31 = 0.0f;
	mtx->m32 = 0.0f;
	mtx->m33 = 1.0f;
	mtx->m34 = 0.0f;
	mtx->m41 = 0.0f;
	mtx->m42 = 0.0f;
	mtx->m43 = 0.0f;
	mtx->m44 = 1.0f;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalMatrixOrtho(MatrixPtr mtx, float width, float height, float near, float far)
{
	if (mtx == NULL)
		return -1;

	if (width < 1.0f || height < 1.0f)
		return -2;

	mtx->m11 = (2.0f / width);
	mtx->m12 = .0f;
	mtx->m13 = .0f;
	mtx->m14 = .0f;
	mtx->m21 = .0f;
	mtx->m22 = (2.0f / height);
	mtx->m23 = .0f;
	mtx->m24 = .0f;
	mtx->m31 = .0f;
	mtx->m32 = .0f;
	mtx->m33 = (1.0f / (near - far));
	mtx->m34 = .0f;
	mtx->m41 = -1.0f;
	mtx->m42 = .0f;
	mtx->m43 = (near / (near - far));
	mtx->m44 = 1.0f;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalMatrixPerspective(MatrixPtr mtx, float fov, float aspect, float near, float far)
{
	if (mtx == NULL)
		return -1;

    float scaleY = 1.0f / tanf(((fov * 0.5f) * 3.1415926f) / 180.0f);
    float scaleX = scaleY / aspect;
	mtx->m11 = scaleX;
	mtx->m12 = 0.0f;
	mtx->m13 = 0.0f;
	mtx->m14 = 0.0f;
	mtx->m21 = 0.0f;
	mtx->m22 = scaleY;
	mtx->m23 = 0.0f;
	mtx->m24 = 0.0f;
	mtx->m31 = 0.0f;
	mtx->m32 = 0.0f;
	mtx->m33 = (far / (near - far));
	mtx->m34 = -1.0f;
	mtx->m41 = 0.0f;
	mtx->m42 = 0.0f;
	mtx->m43 = ((far * near) / (near - far));
	mtx->m44 = 0.0f;

	return 0;
}
